﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Threading;
using MetaView;

namespace MetaView.ManualFit
{
    public partial class Form1 : Form
    {
        VTKControl vtkControl = new VTKControl();
        public Form1()
        {
            InitializeComponent();
            this.Size = new Size(Convert.ToInt16(Screen.PrimaryScreen.WorkingArea.Width * 0.95), Convert.ToInt16(Screen.PrimaryScreen.WorkingArea.Height * 0.95));            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            splitContainer1.SplitterDistance = splitContainer1.Height / 2;
            splitContainer2.SplitterDistance = splitContainer2.Width / 3;
            splitContainer3.SplitterDistance = splitContainer3.Width / 2;


            dataViewer.Updated += new EventHandler(UpdateDetected);
            modelViewer.Updated += new EventHandler(UpdateDetected);

            splitContainer2.Panel1.BackgroundImageLayout = ImageLayout.Zoom;

            vtkControl.Dock = DockStyle.Fill;
            splitContainer2.Panel2.Controls.Add(vtkControl);
        }

        delegate void loadVTKDataDelegate();
        void loadVTKData()
        {
           
                if (vtkControl.InvokeRequired)
                {
                    vtkControl.Invoke(new loadVTKDataDelegate(loadVTKData));
                }
                else
                {
                //    while (true)
                //    {
                        vtkControl.Draw(modelViewer.modelVolume.Matrix);
                        vtkControl.Refresh();
                //    }
                }
        }

        bool is3DInitialized = false;
        private void UpdateDetected(object sender, EventArgs e)
        {
            Bitmap bm = modelViewer.GetCurrentBackGround();
            Bitmap bd = dataViewer.GetCurrentBackGround();

            if (!is3DInitialized)
            {
                //pass data to VTK Window
                ThreadStart a = new ThreadStart(loadVTKData);
                Thread t = new Thread(a);
                t.Start();
                is3DInitialized = true;
            }
        
            if (bm == null || bd == null)
                return;

            ColorMatrix cm = new ColorMatrix();
            cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = cm.Matrix44 = 1;
            cm.Matrix33 = opacity;
            ImageAttributes ia = new ImageAttributes();
            ia.SetColorMatrix(cm);
           
            Bitmap b = new Bitmap(bm.Width, bm.Height);
            Graphics g = Graphics.FromImage(b);
            g.DrawImage(bm,new Rectangle(0,0, bm.Width, bm.Height));
            g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
            g.DrawImage(bd, new Rectangle(0, 0, bd.Width, bd.Height), 0, 0, bd.Width, bd.Height, GraphicsUnit.Pixel, ia);
            splitContainer2.Panel1.BackgroundImage = b;

            vtkControl.Update2DImage(b);
        }

        float opacity = 0.8f;
        
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            opacity += 0.02f;
            if (opacity > 1)
                opacity = 1;
            UpdateDetected(null, null);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            opacity -= 0.02f;
            if (opacity < 0)
                opacity = 0;
            UpdateDetected(null, null);
        }
    }

}
